package com.zc58s.redpack;

import com.alibaba.fastjson2.JSONObject;
import com.zc58s.redpack.basic.Basic;
import com.zc58s.utils.JedisUtils;

import java.util.concurrent.CountDownLatch;

/**
 * @author : fujc-dev@qq.com
 */

public class GenRedPack {

    public static void getHongBao() throws InterruptedException {
        JedisUtils jedis = new JedisUtils();
        jedis.flushall();
        final CountDownLatch latch = new CountDownLatch(Basic.threadCount);
        for (int i = 0; i < Basic.threadCount; i++) {
            final int page = i;
            Thread thread = new Thread(() -> {
                // 每个线程大概要创建多少个红包
                int percent = Basic.hongBaoCount / Basic.threadCount;
                JSONObject object = new JSONObject();
                for (int j = page * percent; j < (page + 1) * percent; j++) {
                    object.put("id", "rid_" + j);
                    object.put("money", j);
                    jedis.lpush(Basic.hongbaoPoolKey, object.toJSONString());
                }
                // 发枪器递减
                latch.countDown();
            });
            thread.start();
        }
        latch.await(); //所有线程处理等待状态
    }
}
